package nina.yiyou.salary.input.info

import nina.yiyou.salary.input.util.CellValueGetter
import nina.yiyou.salary.input.util.ExcelReadOpener
import nina.yiyou.salary.input.util.IdMapMaker
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.ss.usermodel.Sheet
import org.apache.poi.ss.usermodel.Workbook

/**
 * 读取公积金表
 *
 * @see FundExcelV2Reader
 */
@Deprecated
class FundExcelReader {

  FundExcelReader(String excelPath, Map<String, Map> basicMap) {
    _excelPath = excelPath
    _basicMap = basicMap
  }

  Map read() {
    Workbook workbook = new ExcelReadOpener(_excelPath).openXls()
    Sheet sheet = workbook.getSheetAt(0)
    Map idMap = new IdMapMaker(_basicMap).make()

    def result = (6..sheet.lastRowNum - 1)
        .collect { sheet.getRow(it) }
        .collect { toLineMap(it) }
        .collect { [it, it[COLUMN_LIST[1][0]]] }
        .findAll { idMap.containsKey(it[1]) }
        .collectEntries { [idMap[it[1]], it[0]] }

    workbook.close()
    return result
  }

  private Map toLineMap(Row row) {
    return COLUMN_LIST.collectEntries { String key, int col ->
      [key, new CellValueGetter(row.getCell(col)).get()]
    }
  }

  private static final List<List> COLUMN_LIST = [
      ['姓名', 2],
      ['身份证号', 5],
      ['个人', 10],
  ]

  private final String _excelPath

  private final Map<String, Map> _basicMap
}
